package com.toddsoft.toddtris.view;

import com.toddsoft.toddtris.model.Block;
import com.toddsoft.toddtris.model.Well;

public abstract class WellRenderer {

    /**
     * the well, next block, lines and time as they appear on screen
     */
    private int screenWell[][] = new int[Well.WELL_WIDTH][Well.WELL_HEIGHT] ;
    private Block screenNextBlock;
    private int screenLines = -1;
    private int screenTimeSecs;

    //draw a block in the required place
    protected abstract void drawElem(int blockNum, int x, int y, Object obj);
    
    //display the next block
    protected abstract void showNext(Block block, Object obj);
    
    //show the background image
    protected abstract void showBackground(Object obj);

    //show the number of lines made
    protected abstract void showLines(int lines, Object obj);

    protected abstract void showTime(int secs, Object obj);

    protected static String formatTime(int secs) {
        if ((secs % 60) < 10) {
            return (int)(secs / 60) + ":0" + (secs % 60);
        } else {
            return (int)(secs / 60) + ":" + (secs % 60);
        }
    }


    /**
     * draw everything
     */
    public void render(Well well, Object obj) {

        showBackground(obj);

        Block nextBlock = well.getNextBlock();
        showNext(nextBlock, obj);
        this.screenNextBlock = nextBlock;

        int lines = (well.getTargetLines() == 0) ? well.getLines()
                                                 : well.getRemainingLines();
        showLines(lines, obj);
        this.screenLines = lines;

        int timeSecs = (int)(well.getRunningTimeInMillis() / 1000);
        showTime(timeSecs, obj);
        screenTimeSecs = timeSecs;

        for (int i = 0; i < Well.WELL_WIDTH; i++ ) {
            for (int j = 0; j < Well.WELL_HEIGHT; j++ ) {
                int elem = well.getElem(i, j);
                drawElem(elem, i, j, obj);
                screenWell[i][j] = elem;
            }
        }
    }

    /**
     * Draw whatever has changed since last draw
     */
    public void update(Well well, Object obj) {

        Block nextBlock = well.getNextBlock();
        if (this.screenNextBlock != nextBlock) {
            showNext(nextBlock, obj);
            this.screenNextBlock = nextBlock;
        }

        int lines = (well.getTargetLines() == 0) ? well.getLines()
                                                 : well.getRemainingLines();
        if (this.screenLines != lines) {
            showLines(lines, obj);
            this.screenLines = lines;
        }

        int timeSecs = (int)(well.getRunningTimeInMillis() / 1000);
        if (timeSecs != screenTimeSecs) {
            showTime(timeSecs, obj);
            screenTimeSecs = timeSecs;
        }

        for (int i = 0; i < Well.WELL_WIDTH; i++ ) {
            for (int j = 0; j < Well.WELL_HEIGHT; j++ ) {
                int elem = well.getElem(i, j);
                if (elem != screenWell[i][j]) {
                    drawElem(elem, i, j, obj);
                    screenWell[i][j] = elem;
                }
            }
        }
    }

}
